/*
 * PurchaseRequest.fx
 * Author : Yu Min-ting
 * Created on 2009-7-27, 11:02:37
 */

package com.sjtu.fx.user;

import com.sjtu.fx.tools.Panel;

import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;

import javafx.scene.control.TextBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

import javafx.scene.layout.VBox;

import com.sjtu.fx.tools.SwingTextArea;

import javafx.scene.control.Button;
import javafx.stage.Alert;

import javafx.scene.layout.HBox;

import javafx.io.http.HttpRequest;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.System;
import org.json.JSONObject;

import javafx.scene.control.Label;

import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;

import com.sjtu.fx.tools.UIAlert;

public class PurchaseRequest extends CustomNode{
    var panel:Panel;
    public var cScore:Number;

    var nameText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "* Book Name: "
    };
    var nameTextBox = TextBox {
        columns:27
        blocksMouse:false
    }

     var isbnText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "Book ISBN:"
    };
    var isbnTextBox = TextBox {
        columns:27
        blocksMouse:false
    }

    var authorText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "* Book Author: "
    };
    var authorTextBox = TextBox {
        columns:27
        blocksMouse:false
    }

    var publisherText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "* Book Publisher: "
    };
    var publisherTextBox = TextBox {
        columns:27
        blocksMouse:false
    }
     var score = Label {
             text: "Score :"
             font: Font {size:10 name: "Verdana"}
             textFill: Color.rgb(11,51,60)
         }
     var group = ToggleGroup{};

      var one = RadioButton {
            text: "1"
            toggleGroup: group
            selected:true
      }
      var two = RadioButton {
            text: "2"
            toggleGroup: group
      }
      var three = RadioButton {
            text: "3"
            toggleGroup: group
      }
       var four = RadioButton {
            text: "4"
            toggleGroup: group
      }
       var five = RadioButton {
            text: "5"
            toggleGroup: group
      }

     var despText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "Description: "
    };
    var despTextBox = SwingTextArea{
        editable:true
        columns:21
        rows:8

    }

    var required = Text {
             content: "* = Required"
             font: Font {size:11 name: "Verdana Bold"}
             fill: Color.RED
             translateX:40
             translateY:430
         }
    var back = Button {
            text: "   Back   "
            action: function() {
                visible = false;
                nameTextBox.text = "";
                authorTextBox.text = "";
                isbnTextBox.text = "";
                publisherTextBox.text = "";
                despTextBox.text = "";
            }
        }

     var alert = UIAlert {
         translateX:75
         translateY:200
         title:"ERROR"
         content:"Please input entire information"
         width:280
         height:120
         visible:false
         opacity:0.9
     }
    var complete = Button {
        text: "Complete"
        action: function() {
            if(nameTextBox.text==""or authorTextBox.text==""
                      or publisherTextBox.text=="")
                {
                  alert.visible = true;
                 //Alert.inform("ERROR","Please input entire information");

                }
            else{
                addBookRequest();
                alert.title = "Success";
                alert.content = "Request Submit successfully!";
                alert.visible = true;
                //Alert.inform("Success","Request Submit successfully!");
                nameTextBox.text = "";
                authorTextBox.text = "";
                isbnTextBox.text = "";
                publisherTextBox.text = "";
                despTextBox.text = "";
                visible = false;
            }
        }
    }

     override protected function create() : Node {
        Group{
            content:[
                 panel = Panel {
                    width:    430
                    height:   550
                    title:    "Request New Books Purchase"
                    translateX: 0
                    translateY: 0
                },
                VBox{
                    spacing: 30
                    translateX:40 translateY: 65
                    content:[
                        nameText,  authorText, 
                        publisherText, isbnText,despText
                        ]
                },
                 VBox{
                    spacing: 18
                    translateX:150 translateY: 60
                    content:[
                        nameTextBox, authorTextBox,
                        publisherTextBox,  isbnTextBox,despTextBox
                    ]
                 },
                 HBox{
                     spacing:30
                     translateX:40 translateY:380
                     content:[score]
                 },
                  HBox{
                     spacing:28
                     translateX:150 translateY:380
                     content:[one,two,three,four,five]
                 },required,
                 HBox{
                     spacing:40
                     translateX:120 translateY:465
                     content:[back ,complete]
                 },alert
            ]
        }
     }

     function addBookRequest(){
         HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/AddPurchaseRequest"
       onOutput: function(os:OutputStream) {
            try {
                var userId:Integer = (this.parent.parent.parent.lookup("loginPanel") as Login).userid;
                System.out.println("ppp{userId}");
                var json:JSONObject = JSONObject{};
                json.put("bookname",nameTextBox.text);
                json.put("comments", despTextBox.text);
                json.put("isbn",isbnTextBox.text);
                json.put("score",group.selectedButton.text);
                json.put("publisher",publisherTextBox.text);
                json.put("author",authorTextBox.text);
                json.put("userId",userId);
    //            System.out.println(passWd);
                var temp:String = "obj={json.toString()}";
         //       System.out.println(temp);
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }
        onInput: function(is: InputStream) {
            try {
                def data:DataInputStream = new DataInputStream(is);
                var result:String = data.readLine();
                alert.title = "Result";
                alert.content = result;
                alert.visible = true;
                //Alert.inform("Result",result);
            } finally {
                is.close();
            }
        }
    }.start();
     }
}
